package xing2387.filtervideoplayer.videopager

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.viewpager2.adapter.FragmentStateAdapter
import xing2387.filtervideoplayer.item.FilterVideoPageFragment

class VideoListAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {

    private val fragmentManager: FragmentManager = activity.supportFragmentManager
    private val videoUrlList = ArrayList<String>()

    fun setVideoUrlList(urlList: List<String>) {
        videoUrlList.clear()
        videoUrlList.addAll(urlList)
        notifyDataSetChanged()
    }

    override fun createFragment(position: Int): Fragment {
        return FilterVideoPageFragment.newInstance(videoUrlList[position])
    }

    override fun getItemCount() = videoUrlList.size

    override fun getItemId(position: Int): Long {
        return super.getItemId(position)
    }

    override fun containsItem(itemId: Long): Boolean {
        return false
    }

    fun getFragmentAtPosition(position: Int): Fragment? {
        return fragmentManager.findFragmentByTag("f$position")
    }

}